<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>购买游戏API测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            margin-bottom: 20px;
        }
        h1 {
            color: #333;
            text-align: center;
            margin-bottom: 30px;
        }
        h2 {
            color: #666;
            border-bottom: 2px solid #007bff;
            padding-bottom: 10px;
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
            color: #555;
        }
        input, select, textarea {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        button {
            background-color: #007bff;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            margin-right: 10px;
            margin-bottom: 10px;
        }
        button:hover {
            background-color: #0056b3;
        }
        .result {
            margin-top: 15px;
            padding: 10px;
            border-radius: 4px;
            white-space: pre-wrap;
            font-family: monospace;
            font-size: 12px;
        }
        .success {
            background-color: #d4edda;
            border: 1px solid #c3e6cb;
            color: #155724;
        }
        .error {
            background-color: #f8d7da;
            border: 1px solid #f5c6cb;
            color: #721c24;
        }
        .info {
            background-color: #d1ecf1;
            border: 1px solid #bee5eb;
            color: #0c5460;
        }
        .api-url {
            background-color: #f8f9fa;
            padding: 10px;
            border-radius: 4px;
            margin-bottom: 10px;
            font-family: monospace;
        }
    </style>
</head>
<body>
    <h1>购买游戏API测试工具</h1>
    
    <!-- 购买单个游戏 -->
    <div class="container">
        <h2>1. 购买单个游戏</h2>
        <div class="api-url">POST /api/purchase/game.do</div>
        
        <div class="form-group">
            <label for="singleUserId">用户ID:</label>
            <input type="number" id="singleUserId" value="1" placeholder="请输入用户ID">
        </div>
        
        <div class="form-group">
            <label for="singleGameId">游戏ID:</label>
            <input type="number" id="singleGameId" value="2" placeholder="请输入游戏ID">
        </div>
        
        <button onclick="purchaseSingleGame()">购买游戏</button>
        <button onclick="purchaseSingleGameJSON()">购买游戏(JSON)</button>
        
        <div id="singleResult" class="result" style="display: none;"></div>
    </div>
    
    <!-- 批量购买游戏 -->
    <div class="container">
        <h2>2. 批量购买游戏</h2>
        <div class="api-url">POST /api/purchase/batch.do</div>
        
        <div class="form-group">
            <label for="batchUserId">用户ID:</label>
            <input type="number" id="batchUserId" value="1" placeholder="请输入用户ID">
        </div>
        
        <div class="form-group">
            <label for="batchGameIds">游戏ID列表 (用逗号分隔):</label>
            <input type="text" id="batchGameIds" value="3,4,5" placeholder="例如: 3,4,5">
        </div>
        
        <button onclick="batchPurchaseGames()">批量购买</button>
        
        <div id="batchResult" class="result" style="display: none;"></div>
    </div>
    
    <!-- 获取购买历史 -->
    <div class="container">
        <h2>3. 获取购买历史</h2>
        <div class="api-url">GET /api/purchase/history.do</div>
        
        <div class="form-group">
            <label for="historyUserId">用户ID:</label>
            <input type="number" id="historyUserId" value="1" placeholder="请输入用户ID">
        </div>
        
        <div class="form-group">
            <label for="historyPage">页码:</label>
            <input type="number" id="historyPage" value="1" placeholder="页码">
        </div>
        
        <div class="form-group">
            <label for="historySize">每页数量:</label>
            <input type="number" id="historySize" value="10" placeholder="每页数量">
        </div>
        
        <button onclick="getPurchaseHistory()">获取购买历史</button>
        
        <div id="historyResult" class="result" style="display: none;"></div>
    </div>
    
    <!-- 快速测试 -->
    <div class="container">
        <h2>4. 快速测试</h2>
        <button onclick="runAllTests()">运行所有测试</button>
        <button onclick="clearAllResults()">清除所有结果</button>
        
        <div id="testResult" class="result" style="display: none;"></div>
    </div>

    <script>
        const BASE_URL = 'http://localhost:8080'; // 根据实际情况修改
        
        // 显示结果
        function showResult(elementId, data, isSuccess = true) {
            const element = document.getElementById(elementId);
            element.style.display = 'block';
            element.className = 'result ' + (isSuccess ? 'success' : 'error');
            element.textContent = typeof data === 'object' ? JSON.stringify(data, null, 2) : data;
        }
        
        // 显示信息
        function showInfo(elementId, message) {
            const element = document.getElementById(elementId);
            element.style.display = 'block';
            element.className = 'result info';
            element.textContent = message;
        }
        
        // 购买单个游戏 (表单参数)
        async function purchaseSingleGame() {
            const userId = document.getElementById('singleUserId').value;
            const gameId = document.getElementById('singleGameId').value;
            
            if (!userId || !gameId) {
                showResult('singleResult', '请填写用户ID和游戏ID', false);
                return;
            }
            
            try {
                const formData = new FormData();
                formData.append('userId', userId);
                formData.append('gameId', gameId);
                
                const response = await fetch(`${BASE_URL}/api/purchase/game.do`, {
                    method: 'POST',
                    body: formData
                });
                
                const result = await response.json();
                showResult('singleResult', result, result.code === 200);
            } catch (error) {
                showResult('singleResult', '请求失败: ' + error.message, false);
            }
        }
        
        // 购买单个游戏 (JSON)
        async function purchaseSingleGameJSON() {
            const userId = document.getElementById('singleUserId').value;
            const gameId = document.getElementById('singleGameId').value;
            
            if (!userId || !gameId) {
                showResult('singleResult', '请填写用户ID和游戏ID', false);
                return;
            }
            
            try {
                const response = await fetch(`${BASE_URL}/api/purchase/game.do`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        userId: parseInt(userId),
                        gameId: parseInt(gameId)
                    })
                });
                
                const result = await response.json();
                showResult('singleResult', result, result.code === 200);
            } catch (error) {
                showResult('singleResult', '请求失败: ' + error.message, false);
            }
        }
        
        // 批量购买游戏
        async function batchPurchaseGames() {
            const userId = document.getElementById('batchUserId').value;
            const gameIdsStr = document.getElementById('batchGameIds').value;
            
            if (!userId || !gameIdsStr) {
                showResult('batchResult', '请填写用户ID和游戏ID列表', false);
                return;
            }
            
            try {
                const gameIds = gameIdsStr.split(',').map(id => parseInt(id.trim()));
                
                const response = await fetch(`${BASE_URL}/api/purchase/batch.do`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        userId: parseInt(userId),
                        gameIds: gameIds
                    })
                });
                
                const result = await response.json();
                showResult('batchResult', result, result.code === 200);
            } catch (error) {
                showResult('batchResult', '请求失败: ' + error.message, false);
            }
        }
        
        // 获取购买历史
        async function getPurchaseHistory() {
            const userId = document.getElementById('historyUserId').value;
            const page = document.getElementById('historyPage').value;
            const size = document.getElementById('historySize').value;
            
            if (!userId) {
                showResult('historyResult', '请填写用户ID', false);
                return;
            }
            
            try {
                const url = `${BASE_URL}/api/purchase/history.do?userId=${userId}&page=${page}&size=${size}`;
                const response = await fetch(url);
                
                const result = await response.json();
                showResult('historyResult', result, result.code === 200);
            } catch (error) {
                showResult('historyResult', '请求失败: ' + error.message, false);
            }
        }
        
        // 运行所有测试
        async function runAllTests() {
            showInfo('testResult', '开始运行测试...\n');
            
            const tests = [
                { name: '获取购买历史', func: getPurchaseHistory },
                { name: '购买单个游戏', func: purchaseSingleGame },
                { name: '批量购买游戏', func: batchPurchaseGames }
            ];
            
            for (const test of tests) {
                try {
                    showInfo('testResult', `正在执行: ${test.name}...\n`);
                    await test.func();
                    await new Promise(resolve => setTimeout(resolve, 1000)); // 等待1秒
                } catch (error) {
                    showResult('testResult', `测试失败: ${test.name} - ${error.message}`, false);
                }
            }
            
            showInfo('testResult', '所有测试完成！');
        }
        
        // 清除所有结果
        function clearAllResults() {
            const resultElements = ['singleResult', 'batchResult', 'historyResult', 'testResult'];
            resultElements.forEach(id => {
                const element = document.getElementById(id);
                element.style.display = 'none';
                element.textContent = '';
            });
        }
        
        // 页面加载完成后的初始化
        document.addEventListener('DOMContentLoaded', function() {
            showInfo('testResult', '测试工具已准备就绪！\n请确保后端服务正在运行，并根据实际情况修改BASE_URL。');
        });
    </script>
</body>
</html> 